package my.vo.voImpl;

import my.enums.Role;
import my.vo.Vo;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;

/**
 * @author 星环
 * @date 2022/10/31 9:42:09
 * @description
 */
//list版
public class User extends Vo {
    private String username;
    private String password;
    private Role role;

    public User(){

    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public User(int id, String username, String password, Role role) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.role = role;
    }



    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Role getRole() {
        return role;
    }

    public void setRole(int role){
        if (role == 1) {
            setRole(Role.ADMIN);
        } else {
            setRole(Role.READER);
        }
    }

    public void setRole(Role role) {
        this.role = role;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        User user = (User) o;
        return id == user.id;
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", role=" + role +
                '}';
    }

    public void setInfo(User user) {
        setUsername(user.getUsername());
        setPassword(user.getPassword());
        setRole(user.getRole());
    }

    @Override
    public void saveBy(FileWriter fileWriter) {
        StringBuilder sb = new StringBuilder();
        sb.append(id).append(" ").
                append(username).append(" ").
                append(password).append(" ").
                append(role).append("\n");
        try {
            fileWriter.write(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean readBy(BufferedReader fileReader) {
        try {
            String s = fileReader.readLine();
            if(s == null){
                return false;
            }
            String[] fields = s.split(" ");
            if(fields.length != 4){
                return false;
            }
            int id = Integer.parseInt(fields[0]);
            setId(id);
            setUsername(fields[1]);
            setPassword(fields[2]);
            setRole(Role.valueOf(fields[3]));
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }


    @Override
    public Vo getEmptyInstance() {
        return new User();
    }
}
